package com.dingwen.sms;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.function.Predicate;

/**
 * <p>
 * HelloServlet
 * </P>
 *
 * @author dingwen
 * @date 2021.10.18 11:39
 */
public class HelloServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 业务逻辑处理
        String methodParameter = req.getParameter("method");
        Predicate<String> addMethodPredicate = "ADD"::equalsIgnoreCase;
        Predicate<String> deleteMethodPredicate = "DELETE"::equalsIgnoreCase;
        HttpSession session = req.getSession();
        if (addMethodPredicate.test(methodParameter)) {
            session.setAttribute("message", "add().");
        } else if (deleteMethodPredicate.test(methodParameter)) {
            session.setAttribute("message", "delete().");
        }

        // 视图跳转
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }
}
